//
//  SFNavigationController.swift
//  SeeFM_Swift
//
//  Created by CBCT_MBP on 2021/5/19.
//

import UIKit

extension SFNavigationController: UINavigationControllerDelegate {

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
    }

}

class SFNavigationController: UINavigationController {
    
    var hideNavigationLine: Bool = true
    var enableSwipeReturn: Bool = true
    
    fileprivate var navigationTransitionAnimationDisable: Bool = false
    fileprivate var isStatusBarHidden: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

        weak var ws = self
        interactivePopGestureRecognizer?.delegate = ws
        delegate = ws
        
    }
    
}

// MARK: - Pop Gesture
extension SFNavigationController: UIGestureRecognizerDelegate {

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if (gestureRecognizer == self.interactivePopGestureRecognizer) {
            //屏蔽调用rootViewController的滑动返回手势
            if self.viewControllers.count < 2 ||
                self.visibleViewController == self.viewControllers.first {
                return false
            }
            if let visiblevc = self.visibleViewController as? SFBaseController {
                return !visiblevc.shouldDisableGestureBack
            }
        }
        if let gsDelegate = self.visibleViewController as? UIGestureRecognizerDelegate {
            return gsDelegate.gestureRecognizerShouldBegin?(gestureRecognizer) ?? true
        }
        return true
    }
}


// MARK: - Rotation
extension SFNavigationController {
    override var shouldAutorotate: Bool {
        return topViewController?.shouldAutorotate ?? false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return topViewController?.supportedInterfaceOrientations ?? .portrait
    }

    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return topViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
    }
}
